package cn.edu.scau.cmi.zhangjunming.abstractFactory;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.zhangjunming.factory.FactoryController;
import cn.edu.scau.cmi.zhangjunming.simpleFactoryMethod.domain.Meat;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class AbstractFactoryController implements Initializable {
	
	private static Stage stage = null;
	private static Scene scene = null;
	@FXML private TextArea board;
	@FXML private Button getButton;
	@FXML private Button returnButton;
	@FXML private ComboBox brandBox;
	@FXML private ComboBox meatBox;
	
	public AbstractFactoryController() {
		// TODO 自动生成的构造函数存根
		
		//brandBox.getItems().remove(0,brandBox.getItems().size());
		//meatBox.getItems().remove(0,brandBox.getItems().size());
		
		//brandBox.getItems().add(new String("Aonong"));
		//brandBox.getItems().add(new String("Tianbang"));
		//brandBox.getItems().add(new String("Wens"));
		
		//meatBox.getItems().add(new String("beef"));
		//meatBox.getItems().add(new String("pork"));
		//meatBox.getItems().add(new String("chicken"));
	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO 自动生成的方法存根
		
	}
	
	static void createScene() {
		if(scene==null) {
			try {
				URL url = new FactoryController().getClass().getResource("AbstractFactory.fxml");
				Parent root = FXMLLoader.load(url);
				scene = new Scene(root);
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static Stage getStage() {
		return stage;
	}

	public static void setStage(Stage stage) {
		AbstractFactoryController.stage = stage;
	}
	
	@FXML private void get_meat() {
		AbstractFactory factory;
		Meat meat;
		int bid = brandBox.getSelectionModel().getSelectedIndex();
		switch(bid) {
		case 0:factory = new AonongFactory(); break;
		case 1:factory = new TianbangFactory(); break;
		case 2:factory = new WensFactory(); break;
		default:
			board.clear();
			board.appendText("There are someting wrong:\ncan't find factory's id");
			return;
		}
		int mid = brandBox.getSelectionModel().getSelectedIndex();
		switch(mid) {
		case 0:meat = factory.getBeef(); break;
		case 1:meat = factory.getPork(); break;
		case 2:meat = factory.getChicken(); break;
		default:
			board.clear();
			board.appendText("There are someting wrong:\ncan't find meat's id");
			return;
		}
		board.appendText(meat.cook()+"\n");
	}
	
	@FXML private void returnMain() {
		Stage stage = (Stage)returnButton.getScene().getWindow();
		stage.close();
	}
}
